//删除好友居中
var windowhei=window.screen.availHeight;
$("#delfriendplace>div").css("marginTop",(windowhei-240)/2+"px");
//模态框淡入淡出
var overflowlist=document.getElementsByClassName("overflow");
var modalbg=document.getElementsByClassName("modal_bg")[0];
var modal=document.getElementsByClassName("action_menu_container")[0];
//var cancel=document.getElementsByClassName("cancel")[0];
var timer;
for(var i=0;i<overflowlist.length;i++){
    overflowlist[i].onclick=function(){
        modalbg.style.display="block";
        modal.style.display="block";
    }
};
//cancel.onclick=function(){
//    if(modalbg.style.display=="block"&&modal.style.display=="block"){
//        modalbg.className+=' animate_out';
//        modal.className+=' animate_out';
//    }
//    timer=setTimeout(changeclass,100);
//};
function changeclass(){
    modalbg.className='modal_bg';
    modal.className='action_menu_container';
    clearTimeout();
    timer=null;
    changedisplay();
};
function changedisplay(){
    modalbg.style.display="none";
    modal.style.display="none";
};
document.getElementsByClassName("mweb_modal_back_button")[0].onclick=function(){
    history.go(-1);
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var urlfrom=window.location.href;
//var regid=/(id=)([a-zA-Z0-9]*)/;
//var id = urlfrom.match(regid).splice("=")[2];
var regotherid=/(otherid=)([a-zA-Z0-9]*)/;
var otherid = urlfrom.match(regotherid).splice("=")[2];
if(otherid==id){
    location.href="setpage.html";
}
//点击展开大图
$(".photo").click(function(){
    if(haveimg=="yes"){
        var imgheight=$("#showimg").height();
        var bigheight=window.screen.availHeight;
        $("#digimgplace").height(bigheight);
        if(bigheight>imgheight){
            $("#showimg").css("marginTop",(bigheight-imgheight)/2);
        }
        $("#digimgplace").css("display","block");
        $("#page_wrapper").css("display","none");
    }
});
//关闭大图
$("#digimgplace").click(function(){
    $("#digimgplace").css("display","none");
    $("#page_wrapper").css("display","block");
});
//经验金钱判断能否进入------------------------------------
function moneytest(e){
    //判断能不能去看详情页
    var euserid= e.dataset.euser;
    var thiseid= e.dataset.eid;
    var score=e.dataset.score;
    $.ajax({
        type:"post",
        datatype:"json",
        url:textexpurl,
        data:{"quserId":euserid,"contentId":thiseid,"userId":id,"score":score},
        success:function(d){
            //console.log(d);
            if(d.Message=="自己的经验"||d.Message=="已付费"||d.Message=="未付费"){
                $.cookie("paynum",thiseid);
                location.href='shareexppage.html?eid='+thiseid;
            }else{
                $("body").css("overflow","hidden");
                $(".moneyinfomob").css("display","block");
            }
        }
    });
}
//编辑资料 页面跳转
//document.getElementById("bjzl").onclick=function(){
//    location.href="mysetpage.html?id="+id;
//};
var haveimg="no";//控制能否点击头像
var mynick="";
var isfriend;
$().ready(function(){
    //用户信息
    var otherpeoplenick="";
    $.ajax({
        type: "post",
        //提交的网址
        url: "http://42.62.66.198:9001/api/puluuser/getuser",
        //提交的数据
        data: { "userId": otherid },
        //返回数据的格式
        datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
        //成功返回之后调用的函数
        success: function (data) {
            //console.log(data.Data);
            $(".page_title").html(data.Data.nick);
            otherpeoplenick=data.Data.nick;
            $("#nickname").html(data.Data.nick);
            if(data.Data.userAva){
                haveimg="yes";
                $(".photo").append("<img class='profile_photo_img' src='http://42.62.66.198:8003/"+data.Data.userAva+"'>");
                $("#digimgplace").css("background-image","url('http://42.62.66.198:8003/"+data.Data.userAva+"')");
            }
            if(data.Data.sex===1){
                document.getElementById("sex1").style.display="inline-block";
            }else if(data.Data.sex===0){
                document.getElementById("sex2").style.display="inline-block";
            }
            $("#cover").html(data.Data.cover);
            $("#myword").html(data.Data.sign);
            $("#educationword").html(data.Data.education);
            var jobs="";
            for(var i= 0;i<data.Data.jobs.length;i++){
                jobs+=data.Data.jobs[i].typename+"&nbsp;&nbsp;"
            }
            $("#myjobs").html(jobs);
            var hobby="";
            for(var r= 0;r<data.Data.hobbys.length;r++){
                hobby+=data.Data.hobbys[r].typename+"&nbsp;&nbsp;"
            }
            var news="";
            if(data.Data.news&&data.Data.news.length>0){
                for(var n= 0;n<data.Data.news.length;n++){
                    news+=data.Data.news[n].typename+"&nbsp;&nbsp;"
                }
            }
            $("#mynews").html(news);
            $("#myhobby").html(hobby);
            $("#visitedcity").html(data.Data.visitedcity);
            //铺路信息
            //$("#scoreword").html(data.Data.score+"分");
            $("#scoreword").html(Math.round(parseFloat(data.Data.score))/100+"元");
            $("#myquestionCount").html(data.Data.myquestionCount+"条");
            $("#myanswerCount").html(data.Data.myanswerCount+"条");
            $("#myexpCount").html(data.Data.myexpCount+"条");
            $("#mycollecttionCount").html(data.Data.mycollecttionCount+"条");
        }
    });
    //自己的信息
    $.ajax({
        type: "post",
        //提交的网址
        url: "http://42.62.66.198:9001/api/puluuser/getuser",
        //提交的数据
        data: { "userId": id },
        //返回数据的格式
        datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
        //成功返回之后调用的函数
        success: function (mydata) {
            mynick=mydata.Data.nick;
        }
    });
    //最新动态列表页
    $("#lastinfopage").click(function(){
		$.cookie("otherid",otherid);
        location.href="lastinfo.html?"+isfriend;
    });
    //最新动态
    //function newdata(){
    //    $.ajax({
    //        type:"get",
    //        datatype:"json",
    //        url:"http://42.62.66.198:9001/api/PuluUser/GetUserStatusByUserId?userId="+otherid+"&currentIndex=1&pageSize=10",
    //        success:function(data){
    //            console.log("最新动态");
    //            console.log(data);
    //            //console.log(data);
    //            $("#setpagenewslist div").remove();
    //            for(var i=0;i<data.Data.length;i++){
    //                var des="";
    //                if(data.Data[i].des||data.Data[i].answerContent){
    //                    var word=data.Data[i].des||data.Data[i].answerContent;
    //                    des="<div class='desword' style='padding-bottom:5px;'>"+word+"</div>";
    //                };
    //                var score="";
    //                if(data.Data[i].score){
    //                    score="<span class='scoreword'>"+data.Data[i].score+"</span>";
    //                }
    //                if(data.Data[i].contenttypeName==="经验"){
    //                    var tim=getDateDiff(getDateTimeStamp(data.Data[i].time));
    //                    $("#setpagenewslist").append("<div class='pagedlist_item'> <div class='feed_item feed_type_question'> <div class='EventHeader'> <span>"+otherpeoplenick+data.Data[i].operatetypeName+"了此"+data.Data[i].contenttypeName+"</span>"+score+"<span class='rt'>"+tim+"</span> </div>"+des+"<h2 class='no_highlight'> <a class='question_link' onclick='moneytest(this)' data-euser='"+data.Data[i].userId+"' data-eid='"+data.Data[i].contentid+"' data-score='"+data.Data[i].score+"'>"+data.Data[i].content+"</a></h2><div class='ContentFooter QuestionFooter'> <span class='answer_count'>浏览</span> <span class='answer_count'>"+data.Data[i].count+"</span> </div> </div> </div>");
    //                }else if(data.Data[i].contenttypeName==="问题"){
    //                    var hrefto="href='questiondetail.html?qid="+data.Data[i].contentid+"'";
    //                    var tim=getDateDiff(getDateTimeStamp(data.Data[i].time));
    //                    $("#setpagenewslist").append("<div class='pagedlist_item'> <div class='feed_item feed_type_question'> <div class='EventHeader'> <span>"+otherpeoplenick+data.Data[i].operatetypeName+"了此"+data.Data[i].contenttypeName+"</span>"+score+"<span class='rt'>"+tim+"</span> </div>"+des+"<h2 class='no_highlight'> <a class='question_link'"+hrefto+">"+data.Data[i].content+"</a> </h2> <div class='ContentFooter QuestionFooter'> <span class='answer_count'>回答</span> <span class='answer_count'>"+data.Data[i].count+"</span> </div> </div> </div>");
    //                }else if(data.Data[i].contenttypeName==="新闻"){
    //                    var hrefto="href='"+data.Data[i].url+"'";
    //                    //var tim=data.Data[i].time.match(/([0-9\-]+)/)[0];
    //                    var tim=getDateDiff(getDateTimeStamp(data.Data[i].time));
    //                    $("#setpagenewslist").append("<div class='pagedlist_item'> <div class='feed_item feed_type_question'> <div class='EventHeader'> <span>"+otherpeoplenick+data.Data[i].operatetypeName+"了此"+data.Data[i].contenttypeName+"</span> <span class='rt'>"+tim+"</span> </div>"+des+"<h2 class='no_highlight'> <a class='question_link'"+hrefto+">"+data.Data[i].content+"</a> </h2> <div class='ContentFooter QuestionFooter'> <span class='answer_count'>浏览</span> <span class='answer_count'>"+data.Data[i].count+"</span> </div> </div> </div>");
    //                }
    //            }
    //        }
    //    });
    //}
    //获取好友列表
    $.ajax({
        datatype:"json",
        type:"get",
        url:"http://42.62.66.198:9001/api/PuluGoodFriend/GetByUserId?userId="+otherid,
        success:function(d){
            //console.log("别人的好友列表");
            console.log(d);
            if(d.Data.length>0){
                for(var i=0;i<d.Data.length;i++){
                    //已经加好友了
                    if(d.Data[i].friendId===id){
						isfriend="isfriend";
                        console.log("已经是好友了");
                        $("#otherset").css("display","block");
                        $("#otherpageaddfbtn").css("display","none");
                        $("#otherpagedeletebtn").css("display","block");
                      
                        //最新动态列表页箭头显示
                        $("#lastinfopage").css("display","block");
                        //判断是否为密友
                        var str=d.Data[i].isMiYou.toString();
                        if(str==="false"){
                            $("#otherpageaddgbtn").css("display","block");
                        }else if(str==="true"){
                            $("#otherpageaddgbtn").css("display","none");
                        }
                        break;
                    }else{
						isfriend="isstrange";
                        console.log("还不是好友");
                        $("#otherpageaddfbtn").css("display","block");
                        $("#otherpageaddgbtn").css("display","none");
                        $("#otherpagedeletebtn").css("display","none");
                    }
                }
            }

        }
    });
    //获取不看他的动态
    $.ajax({
        datatype:"json",
        type:"get",
        url:"http://42.62.66.198:9001/api/Notlookit/GetBy2Id?userId="+id+"&friendId="+otherid,
        success:function(d){
            console.log("是否不看他的动态");
            console.log(d);
            var str= d.Data.toString();
            if(str==="false"){
                $("#donotseeheimg1").css("display","block");
                $("#donotseeheimg2").css("display","none");
                //newdata();//获取最新动态
            }else if(str==="true"){
                $("#donotseeheimg1").css("display","none");
                $("#donotseeheimg2").css("display","block");
            }
        }
    });
    //获取不让他看我的动态
    $.ajax({
        datatype:"json",
        type:"get",
        url:"http://42.62.66.198:9001/api/Forbiditlook/GetBy2Id?userId="+id+"&friendId="+otherid,
        success:function(d){
            var str= d.Data.toString();
            if(str==="false"){
                document.getElementById("donotseemeimg1").style.display="block";
                document.getElementById("donotseemeimg2").style.display="none";
            }else if(str==="true"){
                document.getElementById("donotseemeimg1").style.display="none";
                document.getElementById("donotseemeimg2").style.display="block";
            }
        }
    });
    //不看他的动态操作
    document.getElementById("donotseehe").onclick=function(){
        if(document.getElementById("donotseeheimg1").style.display==="block"){
            document.getElementById("donotseeheimg1").style.display="none";
            document.getElementById("donotseeheimg2").style.display="block";
            $.ajax({
                type:"post",
                datatype:"json",
                url:"http://42.62.66.198:9001/api/Notlookit/Addpulu_notlookit",
                data:{"userId":id,"friendId":otherid},
                success:function(d){


                }
            })
        }else if(document.getElementById("donotseeheimg2").style.display==="block"){
            document.getElementById("donotseeheimg1").style.display="block";
            document.getElementById("donotseeheimg2").style.display="none";
            $.ajax({
                type:"post",
                datatype:"json",
                url:"http://42.62.66.198:9001/api/Notlookit/Deletepulu_notlookit",
                data:{"userId":id,"friendId":otherid},
                success:function(d){

                }
            })
        }
    };
    //不让他看我的的动态操作
    document.getElementById("donotseeme").onclick=function(){
        if(document.getElementById("donotseemeimg1").style.display==="block"){
            document.getElementById("donotseemeimg1").style.display="none";
            document.getElementById("donotseemeimg2").style.display="block";
            $.ajax({
                type:"post",
                datatype:"json",
                url:"http://42.62.66.198:9001/api/Forbiditlook/Addpulu_forbiditlook",
                data:{"userId":id,"friendId":otherid},
                success:function(d){

                }
            })
        }else if(document.getElementById("donotseemeimg2").style.display==="block"){
            document.getElementById("donotseemeimg1").style.display="block";
            document.getElementById("donotseemeimg2").style.display="none";
            $.ajax({
                type:"post",
                datatype:"json",
                url:"http://42.62.66.198:9001/api/Forbiditlook/Deletepulu_forbiditlook",
                data:{"userId":id,"friendId":otherid},
                success:function(d){

                }
            })
        }
    };
    //添加密友
    $("#otherpageaddgbtn").click(function(){
        $.ajax({
            type:"post",
            datatype:"json",
            url:addmiyouurl,
            data:{"userId":id,"miyouId":otherid},
            success:function(d){
                $("#sendfriendinfook").css("display","block");
                closefirendnote();
                $("#otherpageaddgbtn").css("display","none");
            }
        })
    });

//环信添加好友请求
    //环信建立连接
    var conn = new WebIM.connection({
        https: WebIM.config.https,
        url: WebIM.config.xmppURL,
        isAutoLogin: WebIM.config.isAutoLogin,
        isMultiLoginSessions: WebIM.config.isMultiLoginSessions
    });
    //登录
    var options = {
        apiUrl: WebIM.config.apiURL,
        user:id,
        pwd:id,
        appKey: "pulu#pulu"
    };
    conn.open(options);
    //添加好友  （确定好友 及向服务器存储信息在个人设置页面）
    $("#otherpageaddfbtn").click(function(){
        //console.log("执行一遍了");
        $("#addinputbox div input").val("我是"+mynick);
        $("#addfriendplace").css("display","block");
        $("#page_wrapper").css("display","none");
    });
    //确定添加好友
    $("#okaddbtn").click(function(){

        if(document.querySelector("#addinputbox div input").value&&document.querySelector("#addinputbox div input").value.length>0&&!document.querySelector("#addinputbox div input").value.match(/\s/g)){
            conn.subscribe({
                to: otherid,
                message:document.querySelector("#addinputbox div input").value
            });
            conn.listen({
                onOpened: function ( message ) {          //连接成功回调
                    //如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
                    conn.setPresence();
                    console.log("连接成功");
                },
                onRoster: function ( message ) {
                    //console.log(message);
                }         //处理好友申请
            });
            $("#sendfriendinfook").css("display","block");
            $("#addfriendplace").css("display","none");
            $("#page_wrapper").css("display","block");
            closefirendnote();
        }else{
            alert("请填写申请信息")
        }
    });
    function closefirendnote(){
        setTimeout(function(){
            $("#sendfriendinfook").css("display","none");
        },1000);
    }
    //取消添加好友
    $("#addfriendgobackbtn").click(function(){
        $("#addfriendplace").css("display","none");
        $("#page_wrapper").css("display","block");
    });
    //删除好友页面
    $("#otherpagedeletebtn").click(function(){
        $("#delfriendplace").css("display","block");
        $("body").css("height",windowhei+"px");
        $("body").css("overflow","hidden");
    });
    //删除好友按钮
    $("#yesdelfriendbtn").click(function(){
        conn.removeRoster({
            to: otherid,
            success: function () {//删除成功
                console.log("成功删除好友");
                conn.unsubscribed({
                    to: otherid
                });
                $.ajax({
                    type:"post",
                    datatype:"json",
                    url:deletefriendurl,
                    data:{"userId":id,"friendId":otherid},
                    success:function(d){
                        console.log("删除好友成功");

                        location.reload();
                    }
                })
            },
            error : function () {}//删除失败
        });
    });
    //取消删除好友
    $("#nodelfriendbtn").click(function(){
        $("#delfriendplace").css("display","none");
        $("body").css("height","auto");
        $("body").css("overflow","auto");
    });
    //聊天跳转
    $("#otherpagechatbtn").click(function(){
        location.href="chatingonepeople.html?otherid="+otherid;
    })
});